<template>
  <div v-show="visible">

    <h3>添加任务</h3>
    <el-form   style="max-height:400px;overflow: auto;"  :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="150px">
        <el-row>
          <el-col :span="12">
              <el-form-item label="任务名称" prop="name">
                <el-input v-model="dataForm.name" placeholder="任务名称"></el-input>
              </el-form-item>
               </el-col>
          <el-col :span="12">
              <el-form-item label="选择路由" prop="routeId">
                <el-select v-model="dataForm.routeId" filterable placeholder="请选择路由">
                  <el-option
                    v-for="(item, index) in routes"
                    :key="index"
                    :label="item.name"
                    :value="item.id">
                  </el-option>
                </el-select>

              </el-form-item>

          </el-col>
          <el-col :span="24">
            <el-form-item label="任务定时时间" prop="result">
              <div style="float: left">
                <el-button @click="open">选择任务定时时间</el-button>
                <!-- 2.注册并且使用 @cronResult="xx" 事件可以接收到最终的表达式-->
                <CronUi ref="CronUi" @cronResult="resultValue"></CronUi>
              </div>
              <el-tag size="medium" style="float: left;height: 100%;margin: 6px 20px;"  v-if="result!=''">{{result}}</el-tag>
            </el-form-item>

          </el-col>

          <el-col :span="12">
                  <el-form-item label="最大尝试次数" prop="maxretry">
                    <el-input-number v-model="dataForm.maxretry" controls-position="right" :min="1"/>
            </el-form-item>
            </el-col>
          <el-col :span="12">
                  <el-form-item label="重试时间间隔" prop="retryinterval">
                    <el-input-number v-model="dataForm.retryinterval" controls-position="right" :min="1"/>
            </el-form-item>
            </el-col>
          <el-col :span="12">
            <el-form-item label="失败日志记录" prop="faillog">
              <el-switch
                :active-value="1"
                :inactive-value="0"
                active-text="是"
                inactive-text="否"
                v-model="dataForm.faillog"></el-switch>
            </el-form-item>
            </el-col>
          <el-col :span="12">
            <el-form-item label="常规日志记录" prop="commonlog">
              <el-switch
                :active-value="1"
                :inactive-value="0"
                active-text="是"
                inactive-text="否"
                v-model="dataForm.commonlog"></el-switch>
            </el-form-item>
            </el-col>
          <el-col :span="24">
            <el-form-item label="描述" prop="description">
              <el-input v-model="dataForm.description" type="textarea" placeholder="请输入描述"
                        :autosize="{minRows: 4, maxRows: 4}" :style="{width: '100%'}"></el-input>
            </el-form-item>
            </el-col>
        </el-row>
    </el-form>
          <div  style="text-align: right">
          <span slot="footer" class="dialog-footer">
            <el-button @click="close">取消</el-button>
            <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
          </span>
          </div>
</div>
</template>

<script>
import CronUi from "../../../components/cron/cron-ui/index";
import CronUiSecond from "../../../components/cron-second/cron-ui/index";
  export default {
    components: {
      CronUi,
      CronUiSecond
    },
    data () {
      return {
        hideTips:false,
        routes:[],
        result: "",
        resultForm: {
          cpmEveryStartCron: "",
          cpmEveryEndCron: ""
        },
        visible: true,
        dataForm: {
          maxretry:1,
          retryinterval:1,
          commonlog:0,
          name:"",
          routeId:null,
          description:"",
          result:"",
          faillog:0
        },
        dataRule: {
          name: [{ required: true, message: '任务名称不能为空', trigger: 'blur' }],
          routeId: [{ required: true, message: '请选择路由', trigger: 'blur' }],
          result: [{ required: true, message: '请选择任务定时时间', trigger: 'blur' }],
        }
      }
    },
    methods: {
      getRoutes(){
        this.$http({
          url: this.$http.adornUrl("/tbssbustask/gettbSsbusRouteList"),
          method: "get"
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.routes=data.list
          }
        });
      },



      close() {
        var data = false;
        this.$emit("handleClose", data)
      },

      open() {
        this.$refs["CronUi"].dialogVisible = true;
      },
      open2() {
        this.$refs["CronUiSecond"].dialogVisible = true;
      },
      // 一次一个表达式---最终产生出来的cron表达式
      resultValue(data) {
        this.result = data;
        this.dataForm.result=data
      },
      // 一次两个表达式---规则配置-确定
      confirmCron(data) {
        this.resultForm.cpmEveryStartCron = data.result;
        this.resultForm.cpmEveryEndCron = data.resultEnd;
      },
      remote (val, time, cb) {
        const msg = `remote called: ${val} ${time}`
        setTimeout(() => {
          // get result for e via http...
          const result = msg
          // ...
          // callback
          cb(result)
        }, 200)
      },
      init (id) {
        this.getRoutes();
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl('/tbssbustask/getById/'+id),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                    this.dataForm=data.data;
                    this.result=data.data.tasktime;
                    this.dataForm.result=data.data.tasktime;
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
              this.$http({
                url: this.$http.adornUrl(`/tbssbustask/${!this.dataForm.id ? 'add' : 'update'}`),
                method: 'post',
                data: this.$http.adornData({
                  'id': this.dataForm.id || undefined,
                  'name': this.dataForm.name,
                  'routeId': this.dataForm.routeId,
                  'tasktime': this.result,
                  'maxretry': this.dataForm.maxretry,
                  'retryinterval': this.dataForm.retryinterval,
                  'faillog': this.dataForm.faillog,
                  'commonlog': this.dataForm.commonlog,
                  'description': this.dataForm.description,
                })
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                  })
                  this.close()
                } else {
                  this.$message.error(data.msg)
                }
              })
          }
        })
      }
    }
  }
</script>
